from django.contrib import admin
from import_export.admin import ImportExportModelAdmin
from apps.putin.models import *


# Register your models here.

class PutinReceiptInline(admin.TabularInline):
    model = PutinReceipt
    extra = 1


class PutinReceiptListInline(admin.TabularInline):
    model = PutinReceiptList
    extra = 10
    style = 'table'


class PutinAdmin(ImportExportModelAdmin):
    list_display = ('my_id', 'date_joined', 'desc')
    list_filter = ['date_joined']
    list_per_page = 10
    search_fields = ['my_id', 'desc']
    inlines = [PutinReceiptInline]


class PutinReceiptAdmin(ImportExportModelAdmin):
    list_display = ('putin', 'receipt_num', 'show_market_image', 'fee', 'get_goods_num', 'get_money', 'desc')
    list_filter = ['market__name']
    list_display_links = ['show_market_image']
    list_per_page = 10
    search_fields = ['putin__my_id', 'receipt_num']
    radio_fields = {"market": admin.HORIZONTAL, }
    inlines = [PutinReceiptListInline]

    def get_money(self, obj):
        receipt_lists = obj.putinreceiptlist_set.all()
        receipt = 0
        for receipt_list in receipt_lists:
            receipt += receipt_list.money
        return receipt

    def get_goods_num(self, obj):
        receipt_lists = obj.putinreceiptlist_set.all()
        goods_num = receipt_lists.__len__()
        return goods_num

    get_goods_num.short_description = "元件种数"
    get_money.short_description = "元件金额"

    def save_formset(self, request, form, formset, change):
        instances = formset.save()
        for instance in instances:
            instance.market_num.goods.stock += instance.number
            instance.market_num.goods.save()
            instance.save()

    def delete_model(self, request, obj):
        receipt_lists = obj.putinreceiptlist_set.all()
        for receipt_list in receipt_lists:
            receipt_list.market_num.goods.stock -= receipt_list.number
            receipt_list.market_num.goods.save()
        obj.delete()


class PutinReceiptListAdmin(ImportExportModelAdmin):
    list_display = ('putin_receipt', 'order', 'get_goods_image', 'market_num', 'show_brand_image', 'brand_num',
                    'number', 'money')
    list_filter = ['putin_receipt__market__name', 'brand__name']
    list_per_page = 30
    list_display_links = ['order']
    ordering = ['putin_receipt', 'order']
    search_fields = ['putin_receipt__putin__my_id', 'putin_receipt__receipt_num', 'market_num__market_num',
                     'brand_num', 'market_num__goods__my_id']

    # def save_model(self, request, obj, form, change):
    #     obj.market_num.goods.stock += obj.number
    #     obj.market_num.goods.save()
    #     obj.save()

    def delete_model(self, request, obj):
        obj.market_num.goods.stock -= obj.number
        obj.market_num.goods.save()
        obj.delete()


admin.site.register(Putin, PutinAdmin)
admin.site.register(PutinReceipt, PutinReceiptAdmin)
admin.site.register(PutinReceiptList, PutinReceiptListAdmin)
